package space

import (
	"src/zeus/iserver"
	"src/zeus/msgdef"
)

// type iSpaceCtrl interface {
// 	updateCoord(iserver.ISpaceEntity)
// }

func (s *Space) refreshEntityState() {

	// s.TravsalEntity(common.EntityType_Player, func(entity iserver.IEntity) {
	// 	iw, ok := entity.(IWatcher)
	// 	if ok {
	// 		iw.reflushStateChangeMsg()
	// 	}
	// })
}

// SetEnable 临时处理
func (s *Space) SetEnable(enable bool) {
	s.Entity.SetEnable(enable)
}

// FireMsg 触发消息
func (s *Space) FireMsg(name string, content interface{}) {
	s.Entity.FireMsg(name, content)
}

// FireRPC 触发RPC消息
func (s *Space) FireRPC(methodName string, data []byte) {
	s.Entity.FireRPC(methodName, data)
}

// RegMsgProc 注册消息处理函数
func (s *Space) RegMsgProc(proc interface{}) {
	s.Entity.RegMsgProc(proc)
}

// DoMsg 消息处理
func (s *Space) DoMsg() {
	s.Entity.DoMsg()
}

// DoNormalMsg 立刻处理消息
func (s *Space) DoNormalMsg(name string, data interface{}) error {
	return s.Entity.DoNormalMsg(name, data)
}

// DoRPCMsg 立刻处理RPC消息
func (s *Space) DoRPCMsg(name string, data []byte) error {
	return s.Entity.DoRPCMsg(name, data)
}

// 转发群体消息
func (s *Space) BroadCastMsgRaw(srvid uint64, srvType uint8, spaceID uint64,
	buff []byte, entityIDs ...uint64) {

	msg := &msgdef.EntitysMsgTransport{
		SrvType:    srvType,
		EntityID:   entityIDs,
		SpaceID:    spaceID,
		MsgContent: buff,
	}
	s.delayedCastMsgs = append(s.delayedCastMsgs, &DelayedCastNotifyMsg{
		SrvID: srvid,
		Msg:   msg,
	})
}

type DelayedCastNotifyMsg struct {
	SrvID uint64
	Msg   *msgdef.EntitysMsgTransport
}

// DoBroadCastMsg 處理場景廣播消息
func (s *Space) DoBroadCastMsg() {
	if len(s.delayedCastMsgs) == 0 {
		return
	}
	for _, task := range s.delayedCastMsgs {
		iserver.GetSrvInst().PostMsgToSrv(task.SrvID, task.Msg)
	}
	// for _, msg := range s.delayedCastMsgs {
	// 	s.TravsalEntity(common.EntityType_Player, func(e iserver.IEntity) {
	// 		ise, ok := e.(iserver.IEntityStateGetter)
	// 		if !ok || ise.GetEntityState() != iserver.Entity_State_Loop {
	// 			return
	// 		}
	// 		if msg.cb != nil && !msg.cb(e) {
	// 			return
	// 		}
	// 		e.Post(iserver.ServerTypeClient, msg.msg)
	// 	})
	// }
	// clear(s.delayedCastMsgs)
	s.delayedCastMsgs = s.delayedCastMsgs[0:0]
}
